namespace CSharpPro._0930;

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

//用户登陆与注册
public class Work6
{
    private static Dictionary<string, string> _users = new Dictionary<string, string>();

        public void Test1(string[] args)
        {
            ShowMainMenu();
        }

        /// <summary>
        /// 显示主菜单并处理用户选择
        /// </summary>
        private static void ShowMainMenu()
        {
            while (true)
            {
                Console.Clear();
                Console.WriteLine("===== 用户系统 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.WriteLine("====================");
                Console.Write("请选择功能 (1-3): ");

                string input = Console.ReadLine();
                if (!int.TryParse(input, out int choice))
                {
                    Console.WriteLine("请输入有效数字！按任意键继续...");
                    Console.ReadKey();
                    continue;
                }

                switch (choice)
                {
                    case 1:
                        Login();
                        break;
                    case 2:
                        Register();
                        break;
                    case 3:
                        Console.WriteLine("谢谢使用，再见！");
                        return;
                    default:
                        Console.WriteLine("请输入1-3之间的数字！按任意键继续...");
                        Console.ReadKey();
                        break;
                }
            }
        }
        
        // 用户注册功能
    
        private static void Register()
        {
            Console.Clear();
            Console.WriteLine("===== 用户注册 =====");

            // 获取并验证用户名
            string username;
            while (true)
            {
                Console.Write("请输入用户名: ");
                username = Console.ReadLine()?.Trim();
                
                var (isValid, message) = ValidateUsername(username);
                if (!isValid)
                {
                    Console.WriteLine($"{message}");
                    continue;
                }

                if (_users.ContainsKey(username))
                {
                    Console.WriteLine("用户名已存在，请选择其他用户名");
                    continue;
                }

                break;
            }

            // 获取并验证密码
            string password;
            while (true)
            {
                Console.Write("请输入密码: ");
                password = ReadPassword();
                
                var (isValid, message) = ValidatePassword(password);
                if (!isValid)
                {
                    Console.WriteLine($"{message}");
                    continue;
                }

                // 验证确认密码
                Console.Write("请确认密码: ");
                string confirmPassword = ReadPassword();
                
                if (password != confirmPassword)
                {
                    Console.WriteLine("密码与确认密码不一致");
                    continue;
                }

                break;
            }

            // 注册用户
            _users.Add(username, password);
            Console.WriteLine("注册成功！按任意键返回主菜单...");
            Console.ReadKey();
        }
        
        /// 用户登录功能
        private static void Login()
        {
            Console.Clear();
            Console.WriteLine("===== 用户登录 =====");

            Console.Write("请输入用户名: ");
            string username = Console.ReadLine()?.Trim();

            Console.Write("请输入密码: ");
            string password = ReadPassword();

            // 验证登录信息
            if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(password))
            {
                Console.WriteLine("用户名或密码不可为空！");
            }
            else if (!_users.ContainsKey(username))
            {
                Console.WriteLine("用户名不存在！");
            }
            else if (_users[username] != password)
            {
                Console.WriteLine("密码错误！");
            }
            else
            {
                Console.WriteLine($"✅ 登录成功！欢迎回来，{username}！");
                Console.WriteLine("进入系统首页...（按任意键返回主菜单）");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("按任意键重新尝试...");
            Console.ReadKey();
            Login(); // 登录失败，重新显示登录界面
        }

        /// 验证用户名是否符合规则
        private static (bool isValid, string message) ValidateUsername(string username)
        {
            if (string.IsNullOrWhiteSpace(username))
            {
                return (false, "用户名不可为空");
            }

            if (username.Length < 3 || username.Length > 15)
            {
                return (false, "用户名长度必须为3-15个字符");
            }

            // 验证只能包含字母、数字和下划线
            if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
            {
                return (false, "用户名只能包含字母、数字和下划线");
            }

            return (true, "用户名有效");
        }
    
        /// 验证密码是否符合规则
        private static (bool isValid, string message) ValidatePassword(string password)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                return (false, "密码不可为空");
            }

            if (password.Length < 6 || password.Length > 20)
            {
                return (false, "密码长度必须为6-20个字符");
            }

            // 验证包含至少1个大写字母、1个小写字母和1个数字
            if (!Regex.IsMatch(password, @"[A-Z]") || 
                !Regex.IsMatch(password, @"[a-z]") || 
                !Regex.IsMatch(password, @"[0-9]"))
            {
                return (false, "密码必须包含至少1个大写字母、1个小写字母和1个数字");
            }

            return (true, "密码有效");
        }

        /// 读取密码，不显示输入的字符
        private static string ReadPassword()
        {
            string password = "";
            ConsoleKeyInfo key;

            do
            {
                key = Console.ReadKey(true);
                
                // 处理退格键
                if (key.Key == ConsoleKey.Backspace && password.Length > 0)
                {
                    password = password.Remove(password.Length - 1);
                    Console.Write("\b \b"); 
                }
                // 处理回车键
                else if (key.Key != ConsoleKey.Enter)
                {
                    password += key.KeyChar;
                    Console.Write("*"); 
                }
            } while (key.Key != ConsoleKey.Enter);

            Console.WriteLine(); 
            return password;
        }
    }
